package com.sijia.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sijia.reggie.comment.R;
import com.sijia.reggie.dto.SetmealDto;
import com.sijia.reggie.entity.Setmeal;
import com.sijia.reggie.entity.SetmealDish;
import com.sijia.reggie.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 杨思佳
 * @version 1.0
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService ss;

    @Autowired
    private CategoryService cs;

    @Autowired
    private DishFlavorService dfs;

    @Autowired
    private DishService ds;

    @Autowired
    private SetmealDishService sds;

    @Autowired
    private CacheManager cacheManager;


    @GetMapping("/page")
    public R<Page<SetmealDto>> getByPage(int page, int pageSize, String findKey) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(findKey != null, Setmeal::getName, findKey);
        wrapper.orderByDesc(Setmeal::getUpdateTime).orderByDesc(Setmeal::getId);
        ss.page(setmealPage, wrapper);

        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        List<Setmeal> records = setmealPage.getRecords();
        ArrayList<SetmealDto> setmealDtos = new ArrayList<>();
        SetmealDto setmealDto = null;
        for (Setmeal s : records) {
            setmealDto = new SetmealDto();
            BeanUtils.copyProperties(s, setmealDto);
            if (cs.getById(s.getCategoryId()) != null) {
                setmealDto.setCategoryName(cs.getById(s.getCategoryId()).getName());
            }
            setmealDtos.add(setmealDto);
        }
        setmealDtoPage.setRecords(setmealDtos);
        return R.success(setmealDtoPage);
    }


    /**
     * 新增套餐
     *
     * @param sd
     * @return
     */
    @CacheEvict(value = "setmealCache", allEntries = true)
    @PostMapping
    public R<String> addSetmeal(@RequestBody SetmealDto sd) {
        return ss.addSetmeal(sd);
    }


    @Cacheable(value = "setmealCache", key = "'setmeal' + #setmealId", condition = "#setmealId != null")
    @GetMapping("/{setmealId}")
    public R<SetmealDto> getById(@PathVariable Long setmealId) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = ss.getById(setmealId);
        BeanUtils.copyProperties(setmeal, setmealDto);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealId);
        List<SetmealDish> setmealDishes = sds.list(queryWrapper);
        setmealDto.setSetmealDishes(setmealDishes);
        return R.success(setmealDto);
    }


    @CacheEvict(value = "setmealCache", allEntries = true)
    @PutMapping
    public R<String> updataSetmeal(@RequestBody SetmealDto sd) {
        return ss.updateSetmeal(sd);
    }


    @CacheEvict(value = "setmealCache", allEntries = true)
    @DeleteMapping
    public R<String> deleteSetmeal(Long[] ids) {
        return ss.deleteSetmeal(ids);
    }


    @CacheEvict(value = "setmealCache", allEntries = true)
    @PostMapping("/status/{status}")
    public R<String> changeStatus(@PathVariable int status, Long[] ids) {
        for (long id : ids) {
            Setmeal sm = ss.getById(id);
            sm.setStatus(status);
            ss.updateById(sm);
        }
        if (status == 0) {
            return R.success("套餐停售成功");
        } else {
            return R.success("套餐起售成功");
        }
    }


    /**
     * 根据条件（如分类id）查询套餐信息
     *
     * @param setmeal
     * @return
     */
    @Cacheable(value = "setmealCache", key = "'setmeal' + #setmeal.categoryId + '_' + #setmeal.status", condition = "#setmeal != null")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> Setmeals = ss.list(queryWrapper);
        return R.success(Setmeals);

    }

    @Cacheable(value = "setmealCache", key = "'setmeal' + #setmealId", condition = "#setmeal != null")
    @GetMapping("/dish/{setmealId}")
    public R<SetmealDto> getSetmeal(@PathVariable Long setmealId) {
        Setmeal sm = ss.getById(setmealId);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(sm, setmealDto);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealId);
        List<SetmealDish> setmealDishes = sds.list(queryWrapper);
        setmealDto.setSetmealDishes(setmealDishes);
        return R.success(setmealDto);
    }

}
